package com.bridge.common.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bridge.common.R;


/**
 * Created by Administrator on 2017/12/4.
 * 确认dialog
 */

public class ConfirmDialog extends Dialog {
    private DialogViewListener listener;
    private DialogViewCancleListener listener2;
    private Activity mContext;

    private TextView tvContent;
    private TextView tvCancle;
    private TextView tvSure;

    private String content;
    private String cancleString;
    private String sureString;

    public interface DialogViewListener {
        void sureClick();
    }

    public interface DialogViewCancleListener {
        void cancleClick();
    }

    public ConfirmDialog(Activity context) {
        super(context);
        mContext = context;
    }

    public void setDialogViewListener(DialogViewListener listener) {
        this.listener = listener;
    }

    public void setListener2(DialogViewCancleListener listener2) {
        this.listener2 = listener2;
    }

    public ConfirmDialog(Activity context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    public ConfirmDialog(Activity context, int themeResId, String content) {
        super(context, themeResId);
        mContext = context;
        this.content = content;
    }

    public ConfirmDialog(Activity context, int themeResId, String content, String sureString) {
        super(context, themeResId);
        mContext = context;
        this.content = content;
        this.sureString = sureString;
    }

    public ConfirmDialog(Activity context, int themeResId, String content, String cancleString, String sureString) {
        super(context, themeResId);
        mContext = context;
        this.content = content;
        this.cancleString = cancleString;
        this.sureString = sureString;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_confirm, null);
        setContentView(view);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体中间弹出
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setBackgroundDrawableResource(R.color.transparent);
        //解决弹出被底部导航栏遮挡问题
        dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //消除边距
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //设置Dialog点击外部消失
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        lp.width = (int) (wm.getDefaultDisplay().getWidth() * 0.8);
//        lp.width = ScreenUtils.dip2px(mContext,300);
        lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
//        lp.y =  ScreenUtils.dip2px(mContext,50);
        //将属性设置给窗体
        dialogWindow.setAttributes(lp);

        tvContent = (TextView) findViewById(R.id.tv_content);
        tvCancle = (TextView) findViewById(R.id.tv_cancle);
        tvSure = (TextView) findViewById(R.id.tv_sure);

        if (!TextUtils.isEmpty(content)) {
            tvContent.setText(content);
        }

        if (!TextUtils.isEmpty(cancleString)) {
            tvCancle.setText(cancleString);
        }
        if (!TextUtils.isEmpty(sureString)) {
            tvSure.setText(sureString);
        }
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.sureClick();
                }
                cancel();
            }
        });
        tvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener2 != null) {
                    listener2.cancleClick();
                }
                cancel();
            }
        });
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

}
